package cn.wolfcode.web.controller;

import cn.wolfcode.common.constants.CommonConstants;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.DateUtil;
import cn.wolfcode.util.UserUtil;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by lanxw
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInfoController {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IOrderInfoService orderInfoService;

    @RequestMapping("/doSeckill")
    @RequireLogin
    public Result<String> doSeckill(Integer time, Long seckillId, HttpServletRequest request){
        SeckillProductVo vo = seckillProductService.findFromCache(time, seckillId);
        // 判断下单时间合法性
        /*if (!DateUtil.isLegalTime(vo.getStartDate(),vo.getTime())) {
            return Result.error(CommonCodeMsg.ILLEGAL_OPERATION);
        }*/
        // 判断是否重复下单
        String token = request.getHeader(CommonConstants.TOKEN_NAME);
        String phone = UserUtil.getUserPhone(redisTemplate, token);
        String orderSetKey = SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(String.valueOf(seckillId));
        if (redisTemplate.opsForSet().isMember(orderSetKey,phone)) {
            return Result.error(SeckillCodeMsg.REPEAT_SECKILL);
        }
        // 判断库存是否足够
        String stockCountKey = SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(String.valueOf(time));
        if (redisTemplate.opsForHash().increment(stockCountKey,String.valueOf(seckillId),-1) < 0) {
            return Result.error(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        // -----------
        // 库存扣减
        // 生成订单
        // --------------
        // 使用消息中间件实现异步下单
        OrderMessage msg = new OrderMessage(time, seckillId, token, Long.valueOf(phone));
        rocketMQTemplate.syncSend(MQConstant.ORDER_PEDDING_TOPIC,msg);
        return Result.success("您已进入抢购队伍,请耐心等待请购结果!!!");
    }

    @RequestMapping("/find")
    @RequireLogin
    public Result<OrderInfo> find(String orderNo,HttpServletRequest request){
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        // 获取当前登录用户 phone
        String token = request.getHeader(CommonConstants.TOKEN_NAME);
        String phone = UserUtil.getUserPhone(redisTemplate, token);
        // 判断当前订单是否属于当前用户
        if (!phone.equals(String.valueOf(orderInfo.getUserId()))){
            return Result.error(CommonCodeMsg.ILLEGAL_OPERATION);
        }
        return Result.success(orderInfo);
    }
}
